package com.example.personalinfoapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class ProfileEditActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_INTERESTS = 1;
    private TextView tvUsername;
    private TextView tvInterests;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_edit);

        // 获取用户名
        username = getIntent().getStringExtra("username");

        // 初始化视图
        tvUsername = findViewById(R.id.tv_username);
        tvInterests = findViewById(R.id.tv_interests);

        // 显示用户名
        tvUsername.setText("用户名: " + username);

        // 返回按钮
        Button btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(v -> finish());

        // 设置返回按钮的点击反馈
        btnBack.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setAlpha(0.5f);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        v.setAlpha(1.0f);
                        break;
                }
                return false;
            }
        });

        // 兴趣选择点击事件
        tvInterests.setOnClickListener(v -> {
            Intent intent = new Intent(ProfileEditActivity.this, InterestSelectionActivity.class);
            startActivityForResult(intent, REQUEST_CODE_INTERESTS);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_INTERESTS && resultCode == RESULT_OK && data != null) {
            String selectedInterests = data.getStringExtra("selectedInterests");
            if (selectedInterests != null && !selectedInterests.isEmpty()) {
                tvInterests.setText("兴趣: " + selectedInterests);
            } else {
                tvInterests.setText("兴趣未选择");
            }
        }
    }
}